package com.googlecode.afx.view.behavior;

import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.Node;

import com.googlecode.afx.view.NodeHandler;
import com.googlecode.afx.view.NodeWrapper;

public class ViewStateListenerNodeHandler implements NodeHandler {

	private ViewStateListener listener;

	private Node root;

	public ViewStateListenerNodeHandler(Node root, ViewStateListener listener) {
		this.root = root;
		this.listener = listener;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void process(Node node) { 
		
		if (NodeWrapper.supportsPrimaryValue(node.getClass())) {
			NodeWrapper wrapper = new NodeWrapper(node);
			if(wrapper.isPrimaryPropertyOfType(ObservableValue.class)) {
				wrapper.getPrimaryObservableValue().addListener(new ViewStateListenerAdapter(root, node, listener));
			}
			if(wrapper.isPrimaryPropertyOfType(ObservableList.class)) {
				wrapper.getPrimaryObservableList().addListener(new ViewStateListenerAdapter(root, node, listener));
			}
			
		}
	}

}
